﻿// CheckProjectLibrary2Dialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckProjectLibrary2Dialog.h"
#include "afxdialogex.h"

#include "MainFrm.h"
#include "CheckerDoc.h"

// CCheckProjectLibrary2Dialog 对话框

IMPLEMENT_DYNAMIC(CCheckProjectLibrary2Dialog, CDialogEx)

CCheckProjectLibrary2Dialog::CCheckProjectLibrary2Dialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_PROJECT_LIBRARY_2, pParent)
{
	m_nProjCode = -1;
}

CCheckProjectLibrary2Dialog::~CCheckProjectLibrary2Dialog()
{
}

void CCheckProjectLibrary2Dialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID2, m_props);
	DDX_Control(pDX, IDC_LIST_PROJECT, m_list);
}


BEGIN_MESSAGE_MAP(CCheckProjectLibrary2Dialog, CDialogEx)
	ON_NOTIFY(NM_CLICK, IDC_LIST_PROJECT, &CCheckProjectLibrary2Dialog::OnNMClickListProject)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST_PROJECT, &CCheckProjectLibrary2Dialog::OnNMDblclkListProject)

	ON_NOTIFY(NM_RCLICK, IDC_LIST_PROJECT, &CCheckProjectLibrary2Dialog::OnNMRClickListProject)
	ON_COMMAND(ID_RMENU_PROJECT_LIBRARY_REMOVE, &CCheckProjectLibrary2Dialog::OnRmenuProjectLibraryRemove)
END_MESSAGE_MAP()


// CCheckProjectLibrary2Dialog 消息处理程序

BOOL CCheckProjectLibrary2Dialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_list.AttachBitmap();
	m_list.Populate();

	if (m_list.GetItemCount() > 0)
	{
		PopulateProjectProperty(0);
	}

	AdjustLayout();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCheckProjectLibrary2Dialog::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	AdjustLayout();

	// TODO: 在此处添加消息处理程序代码
}

void CCheckProjectLibrary2Dialog::AdjustLayout()
{
	CWnd* pWnd = GetDlgItem(IDC_STATIC_PROPS);
	if (pWnd)
	{
		CRect rect;
		GetDlgItem(IDC_STATIC_PROPS)->GetWindowRect(rect);
		ScreenToClient(rect);
		int margin = 2;
		rect.left += margin;
		rect.right -= margin;
		rect.top += margin;
		rect.bottom -= margin;
		m_props.MoveWindow(rect);
	}
}

void CCheckProjectLibrary2Dialog::OnNMClickListProject(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}
	PopulateProjectProperty(nItem);

	*pResult = 0;
}

void CCheckProjectLibrary2Dialog::PopulateProjectProperty(int nProjItem)
{
	m_nProjCode = m_list.GetItemData(nProjItem);

	ck::CheckProject* pProject = NULL;
	ck::CheckProjectManager* pProjectManager = ck::CheckProjectManager::GetInstance();
	pProject = pProjectManager->GetProject(m_nProjCode);
	if (pProject)
	{
		m_props.Populate(pProject);

		pProject->Release();
	}
}

z_int32 CCheckProjectLibrary2Dialog::GetSelectedProjectCode()
{
	return m_nProjCode;
}

void CCheckProjectLibrary2Dialog::OnNMDblclkListProject(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}


void CCheckProjectLibrary2Dialog::OnNMRClickListProject(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}

	m_list.SetItemState(nItem, LVNI_FOCUSED | LVIS_SELECTED, LVNI_FOCUSED | LVIS_SELECTED);
	
	CPoint pt, spt;
	GetCursorPos(&pt);
	spt = pt;
	ScreenToClient(&pt);

	CMenu m, *mn;
	m.LoadMenu(IDR_RMENU_PROJECT_LIBRARY_LIST);//加载菜单资源
	mn = m.GetSubMenu(0);//获取菜单子项
	CMenu* pSubMenu = mn->GetSubMenu(0);
	mn->TrackPopupMenu(TPM_LEFTALIGN, spt.x, spt.y, this);    //显示菜单

	*pResult = 0;
}


void CCheckProjectLibrary2Dialog::OnRmenuProjectLibraryRemove()
{
	// TODO: 在此添加命令处理程序代码
	int nItem = m_list.GetSelectionMark();
	if (nItem < 0)
	{
		AfxMessageBox("未选中工程");
		return;
	}

	z_uint32 nProjCode = m_list.GetItemData(nItem);

	CheckerDoc* pDoc = (CheckerDoc*)(((CMainFrame*)AfxGetMainWnd())->GetActiveDocument());
	ck::CheckProject* pProject = pDoc->GetProject();
	if (pProject)
	{
		if (pProject->GetCode() == nProjCode)
		{
			AfxMessageBox("当前检查工程已经打开，请先关闭检查工程");
			return;
		}
	}

	int ret = ::MessageBox(NULL, "与当前工程相关联的所有信息将全部删除，是否删除工程?", "删除工程", MB_OKCANCEL);
	if (ret == IDCANCEL)
	{
		return;
	}

	ck::CheckProjectManager* pProjManager = NULL;
	pProjManager = ck::CheckProjectManager::GetInstance();
	RESULTCODE rc = pProjManager->RemoveProject(nProjCode);
	if (rc != RC_SUCCESS)
	{
		AfxMessageBox("删除工程失败");
	}
	else
	{
		m_list.DeleteItem(nItem);
		AfxMessageBox("成功删除检查工程");
	}
	
}
